package com.example.farmeri.ui.users;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.NestedScrollView;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.farmeri.R;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.http.API;
import com.example.farmeri.pojo.FarmDataT;
import com.example.farmeri.pojo.User;
import com.example.farmeri.presenter.user.IViewUserContract;
import com.example.farmeri.presenter.user.ViewUserPresenter;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * 查看用户
 */
public class ViewUserActivity extends BaseActivity<ViewUserPresenter> implements IViewUserContract {
   private PopupWindow pw;

    //编辑模式switch锁定
    //@BindView(R.id.usermanager_switchedit)
    //Switch lock;

    //发送消息
    @BindView(R.id.access_message_switch)
    Switch switch_message;

    //查看湿度
    @BindView(R.id.access_humidity_switch)
    Switch switch_humid;

    //查看光照
    @BindView(R.id.access_sun_switch)
    Switch switch_sunny;

    //查看温度
    @BindView(R.id.access_temperature_switch)
    Switch switch_temp;

    //查看农田主
    @BindView(R.id.access_farmer_switch)
    Switch switch_farm;

    //编辑和查看标题
    @BindView(R.id.ConstraintLayout_usercontent)
    ConstraintLayout constraintLayout;
    @BindView(R.id.checkuser_name_edit)
    TextView userName;
    @BindView(R.id.checkuser_nickname_edit)
    EditText nickName;
    @BindView(R.id.checkuser_credit_edit)
    EditText credit;
    @BindView(R.id.checkuser_identity_edit)
    EditText ident;
    @BindView(R.id.checkuser_address_edit)
    EditText address;
    @BindView(R.id.checkuser_email_edit)
    EditText email;
    @BindView(R.id.checkuser_age_edit)
    EditText age;
    @BindView(R.id.checkuser_nestedScrollview)
    NestedScrollView nestedScrollview;

    private DevicePolicyManager dpm;
    private ComponentName componentName;
    List<View> viewList=new ArrayList<>();
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_user);
        ButterKnife.bind(this);
        initToolBar(R.id.usercheck_toolbar,"查看用户",true,true);
        initData();
        deviceLockUsermanager();

    }

    private void initData() {
        present = new ViewUserPresenter(this);
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        present.getUserInfo(id);
    }



    private void setUserInfo(User user) {
        nickName.setText(user.getNick_name());
        userName.setText(user.getLogin_name());
        email.setText(user.getEmail());
        credit.setText(""+user.getCredit());
        age.setText(user.getAge());
        address.setText(user.getRequest_place());
        String identString = user.getIdentity();
        ident.setText(identString);
        if (identString.contains("温度管理员")){
            switch_temp.setChecked(true);
        }
        if (identString.contains("湿度管理员")){
            switch_humid.setChecked(true);
        }
        if (identString.contains("光照管理员")){
            switch_sunny.setChecked(true);
        }
        if (identString.contains("消息管理员")){
            switch_message.setChecked(true);
        }
        if (identString.contains("农田主")){
            switch_farm.setChecked(true);
        }
    }


//设置屏幕的编辑和锁定状态
    private void deviceLockUsermanager() {

        //constraintLayout.setOnClickListener(new View.OnClickListener() {
        //    @Override
        //    public void onClick(View v) {
        //
        //    }
        //});
        traversalView(constraintLayout);

        //编辑滑动switch时页面的编辑和只读状态切换
        //lock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        //    @Override
        //    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        //        if(isChecked){
        //           setEditCannotedit(true);
        //        }else{
        //            setEditCannotedit(false);
        //
        //        }
        //    }
        //});

    }


//    //屏蔽点击事件的view，并使其宽高等于父控件RelativeLayout（称之为containerView）的宽高,设置不可编辑
//    private void setCoverIfNotEditable(View containerView,View coverView, boolean editable){
//        if(editable){
//            coverView.setVisibility(View.GONE);
//        }else{
//            containerView.post(new Runnable() {
//                @Override
//                public void run() {
//                    ViewGroup.LayoutParams params = coverView.getLayoutParams();
//                    params.width = containerView.getMeasuredWidth();
//                    params.height = containerView.getMeasuredHeight();
//                    coverView.setLayoutParams(params);
//                    coverView.setVisibility(View.VISIBLE);
//                }
//            });
//        }
//    }

    private void setEditCannotedit(boolean edit) {
        nickName.setEnabled(edit);
        userName.setEnabled(edit);
        address.setEnabled(edit);
        credit.setEnabled(edit);
        age.setEnabled(edit);
        email.setEnabled(edit);
        ident.setEnabled(edit);
        //admin.setEnabled(edit);
        //develop.setEnabled(edit);
        //humid.setEnabled(edit);
        //temp.setEnabled(edit);
        //farm.setEnabled(edit);
        //sunny.setEnabled(edit);
    }

    //获得ConstraintLayout里的所有控件
    private void traversalView(ConstraintLayout constraintLayout) {
        int i =constraintLayout.getChildCount();
        for(int j=0;j<i;j++){
            View view =constraintLayout.getChildAt(j);
                   viewList.add(view);
        }
    }

    @Override
    public void showMessage(String msg) {
        showToast(msg);
    }

    @Override
    public void onUserInfoSuccess(User data) {
        setUserInfo(data);
    }

    @Override
    public void onError(String msg) {
        showToast(msg);
    }
//    //设置不可编辑状态
//    private void setViewsEnable(boolean able) {
//        for (View view : viewList) {
//            view.setEnabled(able);
//            view.setFocusable(able);
//            if (view instanceof EditText) {
//                view.setFocusableInTouchMode(able);
//            }
//            else(view instanceof Switch){
//                view.setFocusableInTouchMode(able);
//           }

//        }


    @Override
    public void onUpdateUserInfoSuccess(String s) {
        showToast("修改成功");
        finish();
    }

    @OnClick(R.id.submit)
    public void submit(){
        Map<String ,String > map = getFormData();
        present.submit(map);
    }


    private Map<String ,String > getFormData() {
        Map<String ,String > map = new HashMap();
        map.put("id",getIntent().getStringExtra("id"));
        map.put("nickName",nickName.getText().toString());
        map.put("credit",credit.getText().toString());
        map.put("address",address.getText().toString());
        map.put("email",email.getText().toString());
        map.put("age",age.getText().toString());
        StringBuilder sb = new StringBuilder();
        if (switch_farm.isChecked()){
            sb.append(",农田主");
        }
        if (switch_humid.isChecked()){
            sb.append(",湿度管理员");
        }
        if (switch_temp.isChecked()){
            sb.append(",温度管理员");
        }
        if (switch_sunny.isChecked()){
            sb.append(",光照管理员");
        }
        if (switch_message.isChecked()){
            sb.append(",消息管理员");
        }
        map.put("ident",sb.subSequence(1,sb.length()).toString());
        return map;
    }


}


